<?php
  require_once('includes/db.php');
  require_once('includes/header.php');
//pr1($_GET);
//pr1($user);

  $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : 0;
  $class = 'success';
  $msg = '';
//pr($_POST);
//pr($_FILES);

  if(isset($_POST['id'])) {

    ######## UPLOAD IMAGE ########
    // If we upload the image
    if($_FILES['image']['error'] != 4)
    {
      $uploadArray = upload($_FILES, 'image', 'uploads/products/');
      $msg = $uploadArray['msg'];
      ##############################
  //pr($uploadArray);
      if($uploadArray['flag'] == 1) {

        ## Creating Thumbnails for NEW Uploaded Image
        ini_set('memory_limit', '-1');
        // include image processing code
        require_once('thumb/image.class.php');

        $img = new Zubrag_image;
        $img->max_x        = 700;
        $img->max_y        = 150;
        $thumb_src = "uploads/products/$uploadArray[file_new_name]";
        $thumb_dest = "uploads/products/thumbs/$uploadArray[file_new_name]";
        $img->GenerateThumbFile($thumb_src,$thumb_dest );//700/50
        ## End Thumb process

        /*Delete old image from file system*/
        unlink('uploads/products/'.$_POST['image_last_name']);
        unlink('uploads/products/thumbs/'.$_POST['image_last_name']);
        unset($_POST['image_last_name']);// don't need to set in DB

        $_POST['image'] = $uploadArray['file_new_name'];
        $updatedRecords = updateById('products',$_POST,$id);
        $msg = "Data Updated Successfully";
        $class = 'success'; // for alert box
      } else {
        $class = 'danger'; // for alert box
      }
    } else { // if image is not uploaded
        $updatedRecords = updateById('products',$_POST,$id);
        $msg = "Data Updated Successfully";
        $class = 'success'; // for alert box
    }

  }

  if($id != 0) {
    $data = fetchById('products',$id);
    if(!$data){redirect('show_all_products.php');}
    $isActive = $data['status']==1 ?'checked':'';
    $isInactive = $data['status']==0 ?'checked':'';
  } else {
    # back to show all products
    redirect('show_all_products.php');
  }
?>

    <div class="container" style="margin-top:30px;">
      <?php if($msg != '') {?>
      <div class="alert alert-<?=$class?> alert-dismissable">
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
        <?=$msg?>
      </div>
      <?php }//end if of alert ?>

    <form method="post" enctype="multipart/form-data" action="" class="form-horizontal" role="form">

<input type="hidden" name="id" value="<?=$data['id']?>" />
<input name="image_last_name" type="hidden" value="<?=$data['image']?>" />


        <h2 class="form-signin-heading">Please edit the details below</h2>

        <div class="form-group">
          <label for="inputName" class="col-lg-2 control-label">Name</label>
          <div class="col-lg-10">
            <input value="<?=$data['name']?>" name="name" type="text" class="form-control" placeholder="My Name is">
          </div>
        </div>


        <div class="form-group">
          <label for="inputEmail1" class="col-lg-2 control-label">Price (INR)</label>
          <div class="col-lg-10">
            <input value="<?=$data['price']?>" name="price" type="text" class="form-control" maxlength="7"  placeholder="0.00">
          </div>
        </div>

        <div class="form-group">
          <label for="inputPhone" class="col-lg-2 control-label">Image</label>
          <div class="col-lg-10">
            <input name="image" type="file" class=""  >

            <img height="90" width="120" src="uploads/products/<?=$data['image']?>" />
          </div>
        </div>

        <div class="form-group">
          <label class="col-lg-2 control-label">Status</label>
          <div class="col-lg-10">

            <div class="radio">
              <label><input type="radio" name="status" id="optionsRadios1" value="1" <?=$isActive?>>Active</label>
            </div>

            <div class="radio">
              <label><input type="radio" name="status" id="optionsRadios1" value="0" <?=$isInactive?> >Inactive</label>
            </div>

          </div>
        </div>

        <div class="form-group">
          <div class="col-lg-offset-2 col-lg-10">
            <input type="submit" class="btn btn-info btn-lg" value="UPDATE !!">
          </div>
        </div>
      </form>
<!------ Form End --------->

    </div> <!-- /container -->
<?php
  require_once('includes/footer.php');
?>
